//
//  ContactCandidateView.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 6/2/14.
//  Copyright (c) 2014 GuangZhouXuanWu. All rights reserved.
//

#import "ContactCandidateView.h"

#define AcceptBtnTitleFormat @"确定(%@)"

#define HEADBTNMARGIN       5

@interface ContactCandidateView ()<XUIButtonDelegate,UIScrollViewDelegate>
{
    UIScrollView *_scroll;
    XUIButton *_acceptBtn;
    XUIButton *_defaultHeadBtn;
    
    NSMutableArray *_acceptContacts;
}

@end

@implementation ContactCandidateView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    UIImageView *backgouendImageView = [[UIImageView alloc]initWithFrame:self.bounds];
    backgouendImageView.image = [[UIImage imageNamed:@"candidate_background.png"] resizeableCenterImage];
    [self addSubview:backgouendImageView];
    
    _acceptBtn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeCustom delegate:self];
   
    //最大标题长度＝确定(99+)
    _acceptBtn.frame = CGRectMake(0, 12, [NSString charSizeWithSystemFontSize:15].width*5, frame.size.height-12*2);
    [_acceptBtn SetFont:[UIFont systemFontOfSize:15]];
    _acceptBtn.right = frame.size.width - 10;
    [_acceptBtn SetTitle:[NSString stringWithFormat:AcceptBtnTitleFormat,@(0)]];
    [_acceptBtn SetTitleColor:[UIColor colorWithR:17 G:130 B:255 A:1]];
    [_acceptBtn SetBackgroundImage:[[UIImage imageNamed:@"btn_blueborder_normal.png"] resizeableCenterImage]];
    _acceptBtn.userInteractionEnabled = NO;
    [self addSubview:_acceptBtn];
    
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 8, _acceptBtn.left-5, frame.size.height-8*2)];
    _scroll.backgroundColor = [UIColor clearColor];
    _scroll.delegate = self;
    _scroll.showsHorizontalScrollIndicator = NO;
    [self addSubview:_scroll];
    
    _defaultHeadBtn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeCustom delegate:self];
    [_defaultHeadBtn SetImage:[UIImage imageNamed:@"candidate_adddefault.png"]];
    _defaultHeadBtn.backgroundColor = [UIColor clearColor];
    _defaultHeadBtn.frame = CGRectMake(HEADBTNMARGIN, 0, _scroll.height, _scroll.height);
    _defaultHeadBtn.szTag = @"default";
    [_scroll addSubview:_defaultHeadBtn];
    
    _acceptContacts = [NSMutableArray new];
    
    return self;
}


-(void)addContactHeadImageWithHeadImage:(UIImage *)headImage andTag:(NSString *)szTag
{
    XUIButton *btn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeCustom delegate:self];
    [btn SetImage:headImage];
    if(headImage == nil)
        [btn SetImage:[UIImage imageNamed:@"img_defaulthead.png"]];
    btn.alpha = 0;
    btn.frame = _defaultHeadBtn.frame;
    btn.szTag = szTag;
    [_scroll addSubview:btn];
    
    [_scroll bringSubviewToFront:_defaultHeadBtn];
    
    [_acceptContacts addObject:btn];
    _acceptBtn.userInteractionEnabled = YES;
    [_acceptBtn SetTitle:[NSString stringWithFormat:AcceptBtnTitleFormat,@(_acceptContacts.count)]];
    
    self.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.25 animations:^
    {
        btn.alpha =1;
        _defaultHeadBtn.left += (HEADBTNMARGIN+_defaultHeadBtn.width);
        
        if(_defaultHeadBtn.right>_scroll.width)
        {
            _scroll.contentSize=CGSizeMake(_defaultHeadBtn.right, 0);
            _scroll.contentOffset=CGPointMake(_scroll.contentSize.width - _scroll.width, 0);
        }
    } completion:^(BOOL finished)
    {
        self.userInteractionEnabled = YES;
    }];
}

-(void)replaceContactHeadImageWithOldTag:(NSString*)oldTag andNewHeadImage:(UIImage*)headImage tag:(NSString*)szTag
{
    for(XUIButton *btn in _acceptContacts)
    {
        if([btn.szTag isEqualToString:oldTag])
        {
            btn.szTag = szTag;
            [btn SetImage:headImage];
            if(headImage == nil)
                [btn SetImage:[UIImage imageNamed:@"img_defaulthead.png"]];
            break;
        }
    }
}

-(void)removeContactHeadImageWithHeadImageTag:(NSString *)szTag
{
    NSArray *arView = _scroll.subviews;
    for(NSUInteger i =0;i<arView.count;i++)
    {
        UIView *removeView = [arView objectAtIndex:i];
        if([removeView isMemberOfClass:[XUIButton class]])
        {
            if([((XUIButton*)removeView).szTag isEqualToString:szTag] == YES)
            {
                __block NSUInteger j= i+1;
                i = arView.count;
                
                [_scroll sendSubviewToBack:removeView];
                
                self.userInteractionEnabled = NO;
                [UIView animateWithDuration:0.25 animations:^
                {
                    removeView.alpha = 0;
                    
                    for(;j<arView.count;j++)
                    {
                        UIView *moveView = [arView objectAtIndex:j];
                        if([moveView isMemberOfClass:[XUIButton class]])
                        {
                            moveView.left -=(HEADBTNMARGIN+_defaultHeadBtn.width);
                        }
                    }
                    if(_defaultHeadBtn.right<_scroll.width)
                    {
                        _scroll.contentSize=CGSizeMake(_defaultHeadBtn.right, 0);
                    }
                    
                } completion:^(BOOL finished)
                {
                    [removeView removeFromSuperview];
                    self.userInteractionEnabled = YES;
                    
                    [_acceptContacts removeObject:removeView];
                    [_acceptBtn SetTitle:[NSString stringWithFormat:AcceptBtnTitleFormat,@(_acceptContacts.count)]];
                    if(_acceptContacts.count == 0)
                        _acceptBtn.userInteractionEnabled = NO;
                        
                }];
            }
        }
    }
}

-(void)clickedUpInsideOfButton:(XUIButton *)btn
{
    if(btn == _acceptBtn)
    {
        NSMutableArray *localPaths = [NSMutableArray new];
        for(UIView *view in _scroll.subviews)
        {
            if([view isMemberOfClass:[XUIButton class]] && [((XUIButton*)view).szTag isEqualToString:@"default"] != YES)
            {
                [localPaths addObject:((XUIButton*)view).szTag];
            }
        }
        [_delegate contactCandidata:self acceptWhenClickAcceptBtn:localPaths];
    }
    else if([btn.szTag isEqualToString:@"default"] !=YES)
    {
        NSString *localPath = btn.szTag;
        [_delegate contactCandidata:self removeHeadImageWhenClick:localPath];
        [self removeContactHeadImageWithHeadImageTag:localPath];
    }
}

-(void)refreshContactHeadImageWithHeadImage:(UIImage*)headImage andTag:(NSString*)szTag
{
    [_scroll.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
    {
        XUIButton *b =obj;
        if([b isMemberOfClass:[XUIButton class]] && [b.szTag isEqualToString:szTag])
        {
            [b SetImage:headImage];
        }
    }];
}

@end
